﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace MemoryTrainer
{
    public partial class MemoryTrainerForm : Form
    {
        private List<MemoryElement> elements = new List<MemoryElement>();
        private List<Control> memoryControls = new List<Control>();
        private int numberOfDistinctElements = 9;
        private int numberOfElements = 9;
        private int timeSpan = 7;
        private Timer timer = new Timer();
        private bool isDragged = false;

        public MemoryTrainerForm()
        {
            InitializeComponent();

            initMemoryElements();
            Width = 680;


            for (int i = 0; i < 3; i++)
                for (int j = 0; j < 3; j++)
                {
                    MemoryButton button = new MemoryButton((i * 3 + j + 1).ToString());
                    button.Location = new Point(40 + j * 80, 80 + i * 80);
                    button.Click += new EventHandler(buttonClicked);
                    //button.MouseDown += new MouseEventHandler(buttonMouseDown);
                    Controls.Add(button);
                }
            buildMemoryElements(true);
            progressBar1.Visible = false;
            buttonDone.Visible = false;
        }

        private void DecreaseProgressBar(object sender, EventArgs e)
        {
            if (progressBar1.Value > 0)
                progressBar1.Value -= 1;
            if (progressBar1.Value == 0)
            {
                timer.Stop();
                foreach (Control control in memoryControls)
                    ((MemoryButton)control).Text = "";
                progressBar1.Visible = false;
                buttonDone.Visible = true;
                this.Update();

            }
        }

        private void buttonClicked(object sender, EventArgs e)
        {
            updateMemoryElements(((MemoryButton)sender).MemoryValue);
        }

        private void buttonMouseDown(object sender, MouseEventArgs e)
        {
            ((Button)sender).DoDragDrop(((Button)sender).Text, DragDropEffects.Move);
        }

        private void updateMemoryElements(string guessedValue)
        {
            foreach (MemoryElement element in elements)
            {
                if (string.IsNullOrEmpty(element.GuessedValue))
                {
                    element.GuessedValue = guessedValue;
                    break;
                }
            }
            buildMemoryElements(true);
        }

        private void initMemoryElements()
        {
            elements.Clear();
            for (int i = 0; i < numberOfElements; i++)
                elements.Add(new MemoryElement());

        }

        private void buildMemoryElements(bool guess)
        {
            
            int row = 1;
            int column = 1;
            int maxNoOfColumns = Convert.ToInt32(Math.Sqrt(numberOfElements));
            foreach (Control control in memoryControls)
            {
                Controls.Remove(control);
            }
            memoryControls.Clear();
            foreach (MemoryElement element in elements)
            {
                string parameter = element.Value;
                if (guess)
                {
                    parameter = element.GuessedValue;
                }
                MemoryButton button = new MemoryButton(parameter);
                button.Location = new Point(320 + column * 80, row * 80);
                button.AllowDrop = true;
                memoryControls.Add(button);
                Controls.Add(button);
                column++;
                if (column > maxNoOfColumns)
                {
                    row++;
                    column = 1;
                }
            }
            Update();
        }

        private void buttonDone_Click(object sender, EventArgs e)
        {
            int count = 0;
            foreach (MemoryElement element in elements)
                if (element.IsCorrect)
                    count++;
            buttonDone.Visible = false;
            labelResult.Text = string.Format("{0} of {1}", count, numberOfElements);
            labelResult.Visible = true;
        }

        private void easyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            numberOfElements = 9;
            timeSpan = 7;
            easyToolStripMenuItem.Checked = true;
            mediumToolStripMenuItem.Checked = false;
            hardToolStripMenuItem.Checked = false;
            initMemoryElements();
            buildMemoryElements(true);
            Width = 680;
        }

        private void mediumToolStripMenuItem_Click(object sender, EventArgs e)
        {
            numberOfElements = 16;
            timeSpan = 13;
            easyToolStripMenuItem.Checked = false;
            mediumToolStripMenuItem.Checked = true;
            hardToolStripMenuItem.Checked = false;
            initMemoryElements();
            buildMemoryElements(true);
            Width = 760;

        }

        private void hardToolStripMenuItem_Click(object sender, EventArgs e)
        {
            numberOfElements = 25;
            timeSpan = 21;
            easyToolStripMenuItem.Checked = false;
            mediumToolStripMenuItem.Checked = false;
            hardToolStripMenuItem.Checked = true;
            initMemoryElements();
            buildMemoryElements(true);
            Width = 840;

        }

        private void startToolStripMenuItem_Click(object sender, EventArgs e)
        {
            progressBar1.Visible = true;
            buttonDone.Visible = false;
            labelResult.Visible = false;

            Random randomNumber = new Random(DateTime.Now.Millisecond);
            elements.Clear();
            for (int i = 0; i < numberOfElements; i++)
                elements.Add(new MemoryElement(randomNumber.Next(numberOfDistinctElements - 1) + 1));
            buildMemoryElements(false);
            progressBar1.Maximum = timeSpan;
            progressBar1.Value = timeSpan;
            timer = new Timer();
            timer.Interval = 1000;
            timer.Tick += new EventHandler(DecreaseProgressBar);
            timer.Start();
        }

    }
}
